home *** CD-ROM | disk | FTP | other *** search
/ The PC-SIG Library 10 / The PC-Sig Library - Shareware for the IBM PC and Compatibles (PC-SIG)(Tenth Edition Disks 1-2804)(1991).iso / PC_SIGCD / 22 / 4 / DISK2247.ZIP / CBASE101.ZIP / BTREE101.ZIP / BTNEXT.C < prev    next >
Text File  |  1990-06-20  |  2KB  |  103 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)btnext.c    1.4 - 90/06/20" */
  5.  
  6. #include <blkio.h>
  7. #include <errno.h>
  8.  
  9. /* local headers */
  10. #include "btree_.h"
  11.  
  12. /*man---------------------------------------------------------------------------
  13. NAME
  14.      btnext - next btree key
  15.  
  16. SYNOPSIS
  17.      #include <btree.h>
  18.  
  19.      int btnext(btp)
  20.      btree_t *btp;
  21.  
  22. DESCRIPTION
  23.      The btnext function advances the cursor of btree btp to the next
  24.      key.  If cursor is currently null, it will be advanced to the
  25.      first key.  If the cursor is currently on the last key, it will
  26.      advance to null.  If the tree is empty, the cursor will remain on
  27.      null.
  28.  
  29.      btnext will fail if one or more of the following is true:
  30.  
  31.      [EINVAL]       btp is not a valid btree pointer.
  32.      [BTELOCK]      btp is not locked.
  33.      [BTENOPEN]     btp is not open.
  34.  
  35. SEE ALSO
  36.      btcursor, btfirst, btlast, btprev.
  37.  
  38. DIAGNOSTICS
  39.      Upon successful completion, a value of 0 is returned.  Otherwise,
  40.      a value of -1 is returned, and errno set to indicate the error.
  41.  
  42. ------------------------------------------------------------------------------*/
  43. int btnext(btp)
  44. btree_t *btp;
  45. {
  46.     int terrno = 0;        /* tmp errno */
  47.  
  48.     /* validate arguments */
  49.     if (!bt_valid(btp)) {
  50.         errno = EINVAL;
  51.         return -1;
  52.     }
  53.  
  54.     /* check if not open */
  55.     if (!(btp->flags & BTOPEN)) {
  56.         errno = BTENOPEN;
  57.         return -1;
  58.     }
  59.  
  60.     /* check locks */
  61.     if (!(btp->flags & BTLOCKS)) {
  62.         errno = BTELOCK;
  63.         return -1;
  64.     }
  65.  
  66.     /* advance cursor to next key in current node */
  67.     if (btp->cbtpos.node != NIL) {
  68.         if (++btp->cbtpos.key <= btp->cbtnp->n) {
  69.             errno = 0;
  70.             return 0;
  71.         }
  72.     }
  73.  
  74.     /* advance cursor to null */
  75.     if (btp->cbtpos.node == btp->bthdr.last) {
  76.         btp->cbtpos.node = NIL;
  77.         btp->cbtpos.key = 0;
  78.         bt_ndinit(btp, btp->cbtnp);
  79.         errno = 0;
  80.         return 0;
  81.     }
  82.  
  83.     /* advance cursor to first key in next node */
  84.     if (btp->cbtpos.node == NIL) {
  85.         btp->cbtpos.node = btp->bthdr.first;
  86.     } else {
  87.         btp->cbtpos.node = btp->cbtnp->rsib;
  88.     }
  89.     if (bt_ndget(btp, btp->cbtpos.node, btp->cbtnp) == -1) {
  90.         BTEPRINT;
  91.         terrno = errno;
  92.         btp->cbtpos.node = NIL;
  93.         btp->cbtpos.key = 0;
  94.         bt_ndinit(btp, btp->cbtnp);
  95.         errno = terrno;
  96.         return -1;
  97.     }
  98.     btp->cbtpos.key = 1;
  99.  
  100.     errno = 0;
  101.     return 0;
  102. }
  103.